const sm4 = require('sm-crypto').sm4;
const sm2 = require('sm-crypto').sm2;
const publicKey = '0450f754c0eb4293e45026735a89f63155a0988c2a25686507f7bd76236940eda8d30623f1a80b0e175139da8b26eb2bfcb4ca42acee2d36275203eb9d67f5c802';
const privateKey = '6db0810be81a3596e26759ab34688e2102bd90787a508975bc68564d36501a1a';
const cipherMode = 1;
export function sm2En(data) {
  // const result = sm2.doEncrypt(data, publicKey, cipherMode);
  const result = sm2.doDecrypt('8f3c8fa7bcefdc6bf0177f0ebfea7551afe7e76786d0cd43bc3e7a8e8230b7c1c1c4c82781a5cb76aa97e00d7adf50d92706c14b21409e0be014521391c2a518d5a16455dad344b1c15a5fe623c29725d1fe477e97313172ee0d889a6d9821bbee8563e1983ebbf852820b033e16a2a9a23c6a7dfd2151b2eff7e364618a13ef96bb33b1e373e148879cfa0b364eabf5e6e21985735a4d3e55bb192cfcb07bdb5ea4e7faef1ed91e41551c1a4ab5e9f772', privateKey, cipherMode);
  return result;
}
export function sm2De(data) {
  const result = sm2.doDecrypt(data, privateKey, cipherMode);
  return result;
}
export function uuid(len, radix) {
  const chars = '0123456789ABCDEF'.split('');
  const uuid = [];
  let i;
  radix = radix || chars.length;
  if (len) {
    // Compact form
    for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
  } else {
    let r;
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
    uuid[14] = '4';
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16);
        uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r];
      }
    }
  }
  return uuid.join('');
}
export function sm4En(word, key) {
  const ecryptedStr = sm4.encrypt(word, key);
  return ecryptedStr;
}
export function sm4De(word, key) {
  const decryptedStr = sm4.decrypt(word, key);
  return decryptedStr;
}
export function smEncryption(word) {
  const sm4DeStr = JSON.stringify(word);
  const sm4Key = uuid(32);
  console.log(sm4Key);
  console.log(sm4DeStr);
  const sm4EnStr = sm4En(sm4DeStr, sm4Key);
  const sm2EnStr = sm2En(sm4Key + ';' + sm4EnStr);
  return sm2EnStr;
}
export function smDecryption(word) {
  const sm2DeStr = sm2De(word);
  console.log(sm2DeStr, 11);
  const content = sm2DeStr.split(';')[1];

  console.log(content, 22);
  const key = sm2DeStr.split(';')[0];
  console.log(key, 33);
  const sm4DeStr = sm4De(content, key);
  console.log(sm4DeStr, 44);
  return sm4DeStr;
}
